package com.cardbaobao.cardbabyclient.widget;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ViewHolder;
import com.cardbaobao.cardbabyclient.model.CreditDetail;
import com.cardbaobao.cardbabyclient.model.FilterBase;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ViewUtil;

import java.util.List;

/**
 * 信用卡详情控件集
 * Created by zad
 * 2016/12/16 10:07
 */

public class CardDetailViewNew{
    /**
     * 信用卡LOGO
     */
    private ImageView mIvCardLogo;
    /**
     * 卡名称
     */
    private TextView mTvCardName;
    /**
     * 信用额度
     */
    private TextView mTvCreditLimit;
    /**
     * 取现额度
     */
    private TextView mTvWithdrawLimit;
    /**
     * 免息期
     */
    private TextView mTvFreeTime;
    /**
     * 申请量
     */
    private TextView mTvApplyNum;
    /**
     * 卡组织名称
     */
    private TextView mTvOrgName;
    /**
     * 卡特点容器
     */
    private LinearLayout mLLFeatureContainer;
    /**
     * 卡特权属性列表容器
     */
    private FlowLayout mFlyPrivilegesContainer;
    /**
     * 点评列表容器
     */
    private LinearLayout mLLCommentContainer;
    private ListView mLvDetail;


    /**
     * 设置ListView 用于动态删除headerView
     */
    public void setListView(ListView mLvDetail) {
        this.mLvDetail = mLvDetail;
    }

    /**
     * 添加View
     *
     * @param parent 父布局
     * @param viewId 子布局Id
     */
    public void addViewById(View parent, int viewId) {
        switch (viewId) {
            case R.id.id_iv_card_logo:
                mIvCardLogo = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_tv_card_name:
                mTvCardName = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_tv_credit_limit:
                mTvCreditLimit = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_tv_withdraw_limit:
                mTvWithdrawLimit = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_tv_free_time:
                mTvFreeTime = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_tv_apply_num:
                mTvApplyNum = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_tv_org_name:
                mTvOrgName = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_ll_Feature_container:
                mLLFeatureContainer = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_fly_PrivilegesList:
                mFlyPrivilegesContainer = ViewUtil.getView(parent, viewId);
                break;
            case R.id.id_ly_container:
                mLLCommentContainer = ViewUtil.getView(parent, viewId);
                break;
        }
    }

    public TextView getTvCardName() {
        return mTvCardName;
    }

    public TextView getTvCreditLimit() {
        return mTvCreditLimit;
    }

    public TextView getTvWithdrawLimit() {
        return mTvWithdrawLimit;
    }

    public TextView getTvFreeTime() {
        return mTvFreeTime;
    }

    public TextView getTvApplyNum() {
        return mTvApplyNum;
    }

    public TextView getTvOrgName() {
        return mTvOrgName;
    }

    public LinearLayout getLLFeatureContainer() {
        return mLLFeatureContainer;
    }

    public static String doubleTrans(float num) {
        if (num % 1.0 == 0) {
            return String.valueOf((long) num);
        }
        return String.valueOf(num);
    }

    /**
     * 设置信用卡详情顶部数据集
     *
     * @param mCreditDetail 详情数据集
     */
    public void setCreditDetailTopData(CreditDetail mCreditDetail) {
        if (mCreditDetail != null) {
            //卡名称
            getTvCardName().setText(mCreditDetail.getName());
            //信用额
//            String creditLimit = (mCreditDetail.getCreditLineMin() == null ? "0" : mCreditDetail.getCreditLineMin()) + "-" + (mCreditDetail.getCreditLineMax() == null ? "0" : mCreditDetail.getCreditLineMax());
            float min = 0.0f;
            float max = 0.0f;
            if (mCreditDetail.getCreditLineMin() != null && !mCreditDetail.getCreditLineMin().equals("")) {
                min = Float.valueOf(mCreditDetail.getCreditLineMin()) / 10000;
            }
            if (mCreditDetail.getCreditLineMax() != null && !mCreditDetail.getCreditLineMax().equals("")) {
                max = Float.valueOf(mCreditDetail.getCreditLineMax()) / 10000;
            }
            getTvCreditLimit().setText(doubleTrans(min) + "-" + doubleTrans(max) + " 万");
//            SpanUtil.setMultipleTextFieldChangeColor(getTvCreditLimit(), mColor, creditLimit);//变色
            //提现额
            String withdrawLimit = (mCreditDetail.getWithdrawalLimit() == null ? 0 : mCreditDetail.getWithdrawalLimit()) + "%";
//            getTvWithdrawLimit().setText(withdrawLimit + " 提现额");
            getTvWithdrawLimit().setText(withdrawLimit);
//            SpanUtil.setMultipleTextFieldChangeColor(getTvWithdrawLimit(), mColor, withdrawLimit);//变色
            //免息期
            String freeMinTime = mCreditDetail.getGracePeriodMin() == null ? "0" : mCreditDetail.getGracePeriodMin();
            String freeMaxTime = mCreditDetail.getGracePeriodMax() == null ? "0" : mCreditDetail.getGracePeriodMax();
            String freeTime = freeMinTime + "-" + freeMaxTime + "天";
//            getTvFreeTime().setText(freeTime + " 免息期");
            getTvFreeTime().setText(freeTime);
//            SpanUtil.setMultipleTextFieldChangeColor(getTvFreeTime(), mColor, freeTime);//变色
            //申请量
            String applyCount = mCreditDetail.getApplyCount();
            getTvApplyNum().setText((applyCount == null ? 0 : applyCount) + "人");
            //普卡 人民币 中国银联
            getTvOrgName().setText(getOrgName(mCreditDetail));
            View parentView = (View) getLLFeatureContainer().getParent();
            if (mCreditDetail.getPrivilegesList() != null && mCreditDetail.getPrivilegesList().size() > 0 || !TextUtils.isEmpty(mCreditDetail.getFeature())) {
                //卡特权属性列表设置
                setCardPrivileges(mCreditDetail);
                //卡特征属性设置
                setCardFeature(mCreditDetail);
                if (parentView != null) {
                    parentView.setVisibility(View.VISIBLE);
                }
            } else {
                if (parentView != null) {
                    parentView.setVisibility(View.GONE);
                }
            }
        }
    }

    /**
     * 设置卡特权列表
     *
     * @param mCreditDetail
     */
    private void setCardPrivileges(CreditDetail mCreditDetail) {
        List<FilterBase> mPrivilegesList = mCreditDetail.getPrivilegesList();
        if (mPrivilegesList != null && mPrivilegesList.size() > 0) {
            mFlyPrivilegesContainer.setAdapter(new CommonAdapter<FilterBase>(mFlyPrivilegesContainer.getContext(), mCreditDetail.getPrivilegesList(), R.layout
                    .layout_credit_detail_attribute_item) {
                @Override
                public void convert(ViewHolder holder, FilterBase item, int position) {
                    holder.setText(R.id.id_tv_item, item.getName());
                }
            });
        } else {
            mFlyPrivilegesContainer.setPadding(0, DensityUtil.dp2px(mFlyPrivilegesContainer.getContext(), 15), 0, 0);
        }
    }

    /**
     * 设置卡特征列表
     */
    private void setCardFeature(CreditDetail mCreditDetail) {
        String featureStr = mCreditDetail.getFeature();
        if (featureStr != null && featureStr.length() > 0 && mLLFeatureContainer != null) {
            mLLFeatureContainer.removeAllViews();
            String[] featureList = null;
            /*if(featureList != null && featureList.length == 1){
                featureList = featureStr.split(",");
            }
            if(featureList != null && featureList.length == 1){
                featureStr = featureStr.replaceAll(" ","");//替换掉空格
                featureStr = featureStr.replaceAll("\\r\\n",",");//替换掉换行符
                featureList = featureStr.split(",");
            }*/
            featureStr = featureStr.replaceAll(" ", "");//替换掉空格
            featureStr = featureStr.replaceAll("\\r\\n", ",");//替换掉换行符
            featureStr = featureStr.replaceAll("，", ",");//替换掉换行符
            featureList = featureStr.split(",");
            if (featureList == null) return;
            LayoutInflater mInflater = LayoutInflater.from(mLLFeatureContainer.getContext());
            for (String feature : featureList) {
                if (!TextUtils.isEmpty(feature)) {
                    View childView = mInflater.inflate(R.layout.layout_credit_detail_attribute_item_tip, null);
                    TextView itemView = ViewUtil.getView(childView, R.id.id_tv_item_feature);
                    itemView.setText(feature);
                    mLLFeatureContainer.addView(childView);
                }
            }
        }
    }

    /**
     * 获取 普卡•人民币•卡组织
     *
     * @param mCreditDetail
     * @return
     */

    private String getOrgName(CreditDetail mCreditDetail) {
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(mCreditDetail.getRankTitle())) {
            sb.append(mCreditDetail.getRankTitle() + " | ");
        }
        if (!TextUtils.isEmpty(mCreditDetail.getCurrencyTitle())) {
            sb.append(mCreditDetail.getCurrencyTitle() + " | ");
        }
        if (!TextUtils.isEmpty(mCreditDetail.getUpperTitle())) {
            sb.append(mCreditDetail.getUpperTitle() + " | ");
        }
        if (!TextUtils.isEmpty(mCreditDetail.getUpperTitle())) {
            sb.append(mCreditDetail.getBottomTitle() + " | ");
        }
        String value = sb.toString();
        int length = value.length();
        if (length > 0) {
            value = value.substring(0, length - 1);
        }
        return value;
    }
}
